home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / gdb-4.12 / gdb / coffread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  57.9 KB  |  2,072 lines

  1. /* Read coff symbol tables and convert to internal format, for GDB.
  2.    Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
  3.    Copyright 1987, 1988, 1989, 1990, 1991, 1992, 1993
  4.    Free Software Foundation, Inc.
  5.  
  6. This file is part of GDB.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "defs.h"
  23. #include "symtab.h"
  24. #include "gdbtypes.h"
  25. #include "breakpoint.h"
  26. #include "bfd.h"
  27. #include "symfile.h"
  28. #include "objfiles.h"
  29. #include "buildsym.h"
  30. #include "gdb-stabs.h"
  31. #include "stabsread.h"
  32. #include "complaints.h"
  33. #include <obstack.h>
  34.  
  35. #include <string.h>
  36.  
  37. #include <time.h> /* For time_t in libbfd.h.  */
  38. #include <sys/types.h> /* For time_t, if not in time.h.  */
  39. #include "libbfd.h"        /* FIXME secret internal data from BFD */
  40. #include "coff/internal.h"    /* Internal format of COFF symbols in BFD */
  41. #include "libcoff.h"        /* FIXME secret internal data from BFD */
  42.  
  43. struct coff_symfile_info {
  44.   file_ptr min_lineno_offset;        /* Where in file lowest line#s are */
  45.   file_ptr max_lineno_offset;        /* 1+last byte of line#s in file */
  46.  
  47.   asection *stabsect;        /* Section pointer for .stab section */
  48.   asection *stabstrsect;        /* Section pointer for .stab section */
  49.   asection *stabindexsect;    /* Section pointer for .stab.index section */
  50.   char *stabstrdata;
  51. };
  52.  
  53. /* Translate an external name string into a user-visible name.  */
  54. #define    EXTERNAL_NAME(string, abfd) \
  55.     (string[0] == bfd_get_symbol_leading_char(abfd)? string+1: string)
  56.  
  57. /* To be an sdb debug type, type must have at least a basic or primary
  58.    derived type.  Using this rather than checking against T_NULL is
  59.    said to prevent core dumps if we try to operate on Michael Bloom
  60.    dbx-in-coff file.  */
  61.  
  62. #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
  63.  
  64. /*
  65.  * Convert from an sdb register number to an internal gdb register number.
  66.  * This should be defined in tm.h, if REGISTER_NAMES is not set up
  67.  * to map one to one onto the sdb register numbers.
  68.  */
  69. #ifndef SDB_REG_TO_REGNUM
  70. # define SDB_REG_TO_REGNUM(value)     (value)
  71. #endif
  72.  
  73. /* Core address of start and end of text of current source file.
  74.    This comes from a ".text" symbol where x_nlinno > 0.  */
  75.  
  76. static CORE_ADDR cur_src_start_addr;
  77. static CORE_ADDR cur_src_end_addr;
  78.  
  79. /* The addresses of the symbol table stream and number of symbols
  80.    of the object file we are reading (as copied into core).  */
  81.  
  82. static GDB_FILE *nlist_stream_global;
  83. static int nlist_nsyms_global;
  84.  
  85. /* Vector of line number information.  */
  86.  
  87. static struct linetable *line_vector;
  88.  
  89. /* Index of next entry to go in line_vector_index.  */
  90.  
  91. static int line_vector_index;
  92.  
  93. /* Last line number recorded in the line vector.  */
  94.  
  95. static int prev_line_number;
  96.  
  97. /* Number of elements allocated for line_vector currently.  */
  98.  
  99. static int line_vector_length;
  100.  
  101. /* Pointers to scratch storage, used for reading raw symbols and auxents.  */
  102.  
  103. static char *temp_sym;
  104. static char *temp_aux;
  105.  
  106. /* Local variables that hold the shift and mask values for the
  107.    COFF file that we are currently reading.  These come back to us
  108.    from BFD, and are referenced by their macro names, as well as
  109.    internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
  110.    macros from ../internalcoff.h .  */
  111.  
  112. static unsigned    local_n_btmask;
  113. static unsigned    local_n_btshft;
  114. static unsigned    local_n_tmask;
  115. static unsigned    local_n_tshift;
  116.  
  117. #define    N_BTMASK    local_n_btmask
  118. #define    N_BTSHFT    local_n_btshft
  119. #define    N_TMASK        local_n_tmask
  120. #define    N_TSHIFT    local_n_tshift
  121.  
  122. /* Local variables that hold the sizes in the file of various COFF structures.
  123.    (We only need to know this to read them from the file -- BFD will then
  124.    translate the data in them, into `internal_xxx' structs in the right
  125.    byte order, alignment, etc.)  */
  126.  
  127. static unsigned    local_linesz;
  128. static unsigned    local_symesz;
  129. static unsigned    local_auxesz;
  130.  
  131.  
  132. /* Chain of typedefs of pointers to empty struct/union types.
  133.    They are chained thru the SYMBOL_VALUE_CHAIN.  */
  134.  
  135. static struct symbol *opaque_type_chain[HASHSIZE];
  136.  
  137. #if 0
  138. /* The type of the function we are currently reading in.  This is
  139.    used by define_symbol to record the type of arguments to a function. */
  140.  
  141. struct type *in_function_type;
  142. #endif
  143.  
  144. struct pending_block *pending_blocks;
  145.  
  146. /* Complaints about various problems in the file being read  */
  147.  
  148. struct complaint ef_complaint = 
  149.   {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
  150.  
  151. struct complaint bf_no_aux_complaint =
  152.   {"`.bf' symbol %d has no aux entry", 0, 0};
  153.  
  154. struct complaint ef_no_aux_complaint =
  155.   {"`.ef' symbol %d has no aux entry", 0, 0};
  156.  
  157. struct complaint lineno_complaint =
  158.   {"Line number pointer %d lower than start of line numbers", 0, 0};
  159.  
  160. struct complaint unexpected_type_complaint =
  161.   {"Unexpected type for symbol %s", 0, 0};
  162.  
  163. struct complaint bad_sclass_complaint =
  164.   {"Bad n_sclass for symbol %s", 0, 0};
  165.  
  166. struct complaint misordered_blocks_complaint =
  167.   {"Blocks out of order at address %x", 0, 0};
  168.  
  169. struct complaint tagndx_bad_complaint =
  170.   {"Symbol table entry for %s has bad tagndx value", 0, 0};
  171.  
  172. struct complaint eb_complaint = 
  173.   {"Mismatched .eb symbol ignored starting at symnum %d", 0, 0};
  174.  
  175. /* Simplified internal version of coff symbol table information */
  176.  
  177. struct coff_symbol {
  178.   char *c_name;
  179.   int c_symnum;        /* symbol number of this entry */
  180.   int c_naux;        /* 0 if syment only, 1 if syment + auxent, etc */
  181.   long c_value;
  182.   int c_sclass;
  183.   int c_secnum;
  184.   unsigned int c_type;
  185. };
  186.  
  187. static struct type *
  188. coff_read_struct_type PARAMS ((int, int, int));
  189.  
  190. static struct type *
  191. decode_base_type PARAMS ((struct coff_symbol *, unsigned int,
  192.               union internal_auxent *));
  193.  
  194. static struct type *
  195. decode_type PARAMS ((struct coff_symbol *, unsigned int,
  196.              union internal_auxent *));
  197.  
  198. static struct type *
  199. decode_function_type PARAMS ((struct coff_symbol *, unsigned int,
  200.                   union internal_auxent *));
  201.  
  202. static struct type *
  203. coff_read_enum_type PARAMS ((int, int, int));
  204.  
  205. static struct symbol *
  206. process_coff_symbol PARAMS ((struct coff_symbol *, union internal_auxent *,
  207.                  struct objfile *));
  208.  
  209. static void
  210. patch_opaque_types PARAMS ((struct symtab *));
  211.  
  212. static void
  213. patch_type PARAMS ((struct type *, struct type *));
  214.  
  215. static void
  216. enter_linenos PARAMS ((long, int, int));
  217.  
  218. static void
  219. free_linetab PARAMS ((void));
  220.  
  221. static int
  222. init_lineno PARAMS ((int, long, int));
  223.  
  224. static char *
  225. getfilename PARAMS ((union internal_auxent *));
  226.  
  227. static char *
  228. getsymname PARAMS ((struct internal_syment *));
  229.  
  230. static void
  231. free_stringtab PARAMS ((void));
  232.  
  233. static int
  234. init_stringtab PARAMS ((int, long));
  235.  
  236. static void
  237. read_one_sym PARAMS ((struct coff_symbol *, struct internal_syment *,
  238.               union internal_auxent *));
  239.  
  240. static void
  241. read_coff_symtab PARAMS ((long, int, struct objfile *));
  242.  
  243. static void
  244. find_linenos PARAMS ((bfd *, sec_ptr, PTR));
  245.  
  246. static void
  247. coff_symfile_init PARAMS ((struct objfile *));
  248.  
  249. static void
  250. coff_new_init PARAMS ((struct objfile *));
  251.  
  252. static void
  253. coff_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
  254.  
  255. static void
  256. coff_symfile_finish PARAMS ((struct objfile *));
  257.  
  258. static void record_minimal_symbol
  259.   PARAMS ((char *, CORE_ADDR, enum minimal_symbol_type, struct objfile *));
  260.  
  261. static void
  262. coff_end_symtab PARAMS ((struct objfile *));
  263.  
  264. static void
  265. complete_symtab PARAMS ((char *, CORE_ADDR, unsigned int));
  266.  
  267. static void
  268. coff_start_symtab PARAMS ((void));
  269.  
  270. static void
  271. coff_record_line PARAMS ((int, CORE_ADDR));
  272.  
  273. static struct type *
  274. coff_alloc_type PARAMS ((int));
  275.  
  276. static struct type **
  277. coff_lookup_type PARAMS ((int));
  278.  
  279.  
  280. static void
  281. coff_locate_sections PARAMS ((bfd *, asection *, PTR));
  282.  
  283. /* We are called once per section from coff_symfile_read.  We
  284.    need to examine each section we are passed, check to see
  285.    if it is something we are interested in processing, and
  286.    if so, stash away some access information for the section.
  287.  
  288.    FIXME: The section names should not be hardwired strings (what
  289.    should they be?  I don't think most object file formats have enough
  290.    section flags to specify what kind of debug section it is
  291.    -kingdon).  */
  292.  
  293. static void
  294. coff_locate_sections (ignore_abfd, sectp, csip)
  295.      bfd *ignore_abfd;
  296.      asection *sectp;
  297.      PTR csip;
  298. {
  299.   register struct coff_symfile_info *csi;
  300.  
  301.   csi = (struct coff_symfile_info *) csip;
  302.   if (STREQ (sectp->name, ".stab"))
  303.     {
  304.       csi->stabsect = sectp;
  305.     }
  306.   else if (STREQ (sectp->name, ".stabstr"))
  307.     {
  308.       csi->stabstrsect = sectp;
  309.     }
  310.   else if (STREQ (sectp->name, ".stab.index"))
  311.     {
  312.       csi->stabindexsect = sectp;
  313.     }
  314. }
  315.  
  316. /* Look up a coff type-number index.  Return the address of the slot
  317.    where the type for that index is stored.
  318.    The type-number is in INDEX. 
  319.  
  320.    This can be used for finding the type associated with that index
  321.    or for associating a new type with the index.  */
  322.  
  323. static struct type **
  324. coff_lookup_type (index)
  325.      register int index;
  326. {
  327.   if (index >= type_vector_length)
  328.     {
  329.       int old_vector_length = type_vector_length;
  330.  
  331.       type_vector_length *= 2;
  332.       if (index /* is still */ >= type_vector_length) {
  333.     type_vector_length = index * 2;
  334.       }
  335.       type_vector = (struct type **)
  336.     xrealloc ((char *) type_vector,
  337.           type_vector_length * sizeof (struct type *));
  338.       memset (&type_vector[old_vector_length], 0,
  339.          (type_vector_length - old_vector_length) * sizeof(struct type *));
  340.     }
  341.   return &type_vector[index];
  342. }
  343.  
  344. /* Make sure there is a type allocated for type number index
  345.    and return the type object.
  346.    This can create an empty (zeroed) type object.  */
  347.  
  348. static struct type *
  349. coff_alloc_type (index)
  350.      int index;
  351. {
  352.   register struct type **type_addr = coff_lookup_type (index);
  353.   register struct type *type = *type_addr;
  354.  
  355.   /* If we are referring to a type not known at all yet,
  356.      allocate an empty type for it.
  357.      We will fill it in later if we find out how.  */
  358.   if (type == NULL)
  359.     {
  360.       type = alloc_type (current_objfile);
  361.       *type_addr = type;
  362.     }
  363.   return type;
  364. }
  365.  
  366. /* Record a line number entry for line LINE at address PC.
  367.    FIXME:  Use record_line instead.  */
  368.  
  369. static void
  370. coff_record_line (line, pc)
  371.      int line;
  372.      CORE_ADDR pc;
  373. {
  374.   struct linetable_entry *e;
  375.   /* Make sure line vector is big enough.  */
  376.  
  377.   if (line_vector_index + 2 >= line_vector_length)
  378.     {
  379.       line_vector_length *= 2;
  380.       line_vector = (struct linetable *)
  381.     xrealloc ((char *) line_vector, sizeof (struct linetable)
  382.           + (line_vector_length
  383.              * sizeof (struct linetable_entry)));
  384.     }
  385.  
  386.   e = line_vector->item + line_vector_index++;
  387.   e->line = line; e->pc = pc;
  388. }
  389.  
  390. /* Start a new symtab for a new source file.
  391.    This is called when a COFF ".file" symbol is seen;
  392.    it indicates the start of data for one original source file.  */
  393.  
  394. static void
  395. coff_start_symtab ()
  396. {
  397.   start_symtab (
  398.         /* We fill in the filename later.  start_symtab
  399.            puts this pointer into last_source file and in
  400.            coff_end_symtab we assume we can free() it.
  401.            FIXME: leaks memory.  */
  402.         savestring ("", 0),
  403.         /* We never know the directory name for COFF.  */
  404.         NULL,
  405.         /* The start address is irrelevant, since we set
  406.            last_source_start_addr in coff_end_symtab.  */
  407.         0);
  408.  
  409.   /* Initialize the source file line number information for this file.  */
  410.  
  411.   if (line_vector)        /* Unlikely, but maybe possible? */
  412.     free ((PTR)line_vector);
  413.   line_vector_index = 0;
  414.   line_vector_length = 1000;
  415.   prev_line_number = -2;    /* Force first line number to be explicit */
  416.   line_vector = (struct linetable *)
  417.     xmalloc (sizeof (struct linetable)
  418.          + line_vector_length * sizeof (struct linetable_entry));
  419. }
  420.  
  421. /* Save the vital information from when starting to read a file,
  422.    for use when closing off the current file.
  423.    NAME is the file name the symbols came from, START_ADDR is the first
  424.    text address for the file, and SIZE is the number of bytes of text.  */
  425.  
  426. static void
  427. complete_symtab (name, start_addr, size)
  428.     char *name;
  429.     CORE_ADDR start_addr;
  430.     unsigned int size;
  431. {
  432.   last_source_file = savestring (name, strlen (name));
  433.   cur_src_start_addr = start_addr;
  434.   cur_src_end_addr = start_addr + size;
  435.  
  436.   if (current_objfile -> ei.entry_point >= cur_src_start_addr &&
  437.       current_objfile -> ei.entry_point <  cur_src_end_addr)
  438.     {
  439.       current_objfile -> ei.entry_file_lowpc = cur_src_start_addr;
  440.       current_objfile -> ei.entry_file_highpc = cur_src_end_addr;
  441.     }
  442. }
  443.  
  444. /* Finish the symbol definitions for one main source file,
  445.    close off all the lexical contexts for that file
  446.    (creating struct block's for them), then make the
  447.    struct symtab for that file and put it in the list of all such. */
  448.  
  449. static void
  450. coff_end_symtab (objfile)
  451.      struct objfile *objfile;
  452. {
  453.   struct symtab *symtab;
  454.  
  455.   last_source_start_addr = cur_src_start_addr;
  456.  
  457.   /* For no good reason, this file stores the number of entries in a
  458.      separate variable instead of in line_vector->nitems.  Fix it.  */
  459.   if (line_vector)
  460.     line_vector->nitems = line_vector_index;
  461.  
  462.   /* For COFF, we only have one subfile, so we can just look at
  463.      subfiles and not worry about there being other elements in the
  464.      chain.  We fill in various fields now because we didn't know them
  465.      before (or because doing it now is simply an artifact of how this
  466.      file used to be written).  */
  467.   subfiles->line_vector = line_vector;
  468.   subfiles->name = last_source_file;
  469.  
  470.   /* sort_pending is needed for amdcoff, at least.
  471.      sort_linevec is needed for the SCO compiler.  */
  472.   symtab = end_symtab (cur_src_end_addr, 1, 1, objfile, 0);
  473.  
  474.   if (symtab != NULL)
  475.     free_named_symtabs (symtab->filename);
  476.  
  477.   /* Reinitialize for beginning of new file. */
  478.   line_vector = 0;
  479.   line_vector_length = -1;
  480.   last_source_file = NULL;
  481. }
  482.  
  483. static void
  484. record_minimal_symbol (name, address, type, objfile)
  485.      char *name;
  486.      CORE_ADDR address;
  487.      enum minimal_symbol_type type;
  488.      struct objfile *objfile;
  489. {
  490.   /* We don't want TDESC entry points in the minimal symbol table */
  491.   if (name[0] == '@') return;
  492.  
  493.   prim_record_minimal_symbol (savestring (name, strlen (name)), address, type,
  494.                   objfile);
  495. }
  496.  
  497. /* coff_symfile_init ()
  498.    is the coff-specific initialization routine for reading symbols.
  499.    It is passed a struct objfile which contains, among other things,
  500.    the BFD for the file whose symbols are being read, and a slot for
  501.    a pointer to "private data" which we fill with cookies and other
  502.    treats for coff_symfile_read ().
  503.  
  504.    We will only be called if this is a COFF or COFF-like file.
  505.    BFD handles figuring out the format of the file, and code in symtab.c
  506.    uses BFD's determination to vector to us.
  507.  
  508.    The ultimate result is a new symtab (or, FIXME, eventually a psymtab).  */
  509.  
  510. static int text_bfd_scnum;
  511.  
  512. static void
  513. coff_symfile_init (objfile)
  514.      struct objfile *objfile;
  515. {
  516.   asection    *section;
  517.   bfd *abfd = objfile->obfd;
  518.  
  519.   /* Allocate struct to keep track of stab reading. */
  520.   objfile->sym_stab_info = (PTR)
  521.     xmmalloc (objfile -> md, sizeof (struct dbx_symfile_info));
  522.  
  523.   memset ((PTR) objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info));
  524.  
  525.   /* Allocate struct to keep track of the symfile */
  526.   objfile -> sym_private = xmmalloc (objfile -> md,
  527.                      sizeof (struct coff_symfile_info));
  528.  
  529.   memset (objfile->sym_private, 0, sizeof (struct coff_symfile_info));
  530.  
  531.   init_entry_point_info (objfile);
  532.  
  533.   /* Save the section number for the text section */
  534.   section = bfd_get_section_by_name (abfd, ".text");
  535.   if (section)
  536.     text_bfd_scnum = section->index;
  537.   else
  538.     text_bfd_scnum = -1;
  539. }
  540.  
  541. /* This function is called for every section; it finds the outer limits
  542.    of the line table (minimum and maximum file offset) so that the
  543.    mainline code can read the whole thing for efficiency.  */
  544.  
  545. /* ARGSUSED */
  546. static void
  547. find_linenos (abfd, asect, vpinfo)
  548.      bfd *abfd;
  549.      sec_ptr asect;
  550.      PTR vpinfo;
  551. {
  552.   struct coff_symfile_info *info;
  553.   int size, count;
  554.   file_ptr offset, maxoff;
  555.  
  556. /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  557.   count = asect->lineno_count;
  558. /* End of warning */
  559.  
  560.   if (count == 0)
  561.     return;
  562.   size = count * local_linesz;
  563.  
  564.   info = (struct coff_symfile_info *)vpinfo;
  565. /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  566.   offset = asect->line_filepos;
  567. /* End of warning */
  568.  
  569.   if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
  570.     info->min_lineno_offset = offset;
  571.  
  572.   maxoff = offset + size;
  573.   if (maxoff > info->max_lineno_offset)
  574.     info->max_lineno_offset = maxoff;
  575. }
  576.  
  577.  
  578. /* The BFD for this file -- only good while we're actively reading
  579.    symbols into a psymtab or a symtab.  */
  580.  
  581. static bfd *symfile_bfd;
  582.  
  583. /* Read a symbol file, after initialization by coff_symfile_init.  */
  584. /* FIXME!  Addr and Mainline are not used yet -- this will not work for
  585.    shared libraries or add_file!  */
  586.  
  587. /* ARGSUSED */
  588. static void
  589. coff_symfile_read (objfile, section_offsets, mainline)
  590.      struct objfile *objfile;
  591.      struct section_offsets *section_offsets;
  592.      int mainline;
  593. {
  594.   struct coff_symfile_info *info;
  595.   struct dbx_symfile_info *dbxinfo;
  596.   bfd *abfd = objfile->obfd;
  597.   coff_data_type *cdata = coff_data (abfd);
  598.   char *name = bfd_get_filename (abfd);
  599.   int desc;
  600.   register int val;
  601.   int num_symbols;
  602.   int symtab_offset;
  603.   int stringtab_offset;
  604.   struct cleanup *back_to;
  605.   int stabsize, stabstrsize;
  606.  
  607.   info = (struct coff_symfile_info *) objfile -> sym_private;
  608.   dbxinfo = (struct dbx_symfile_info *) objfile->sym_stab_info;
  609.   symfile_bfd = abfd;            /* Kludge for swap routines */
  610.  
  611. /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  612.    desc = fileno ((GDB_FILE *)(abfd->iostream));    /* File descriptor */
  613.    num_symbols = bfd_get_symcount (abfd);    /* How many syms */
  614.    symtab_offset = cdata->sym_filepos;        /* Symbol table file offset */
  615.    stringtab_offset = symtab_offset +        /* String table file offset */
  616.               num_symbols * cdata->local_symesz;
  617.  
  618.   /* Set a few file-statics that give us specific information about
  619.      the particular COFF file format we're reading.  */
  620.   local_linesz   = cdata->local_linesz;
  621.   local_n_btmask = cdata->local_n_btmask;
  622.   local_n_btshft = cdata->local_n_btshft;
  623.   local_n_tmask  = cdata->local_n_tmask;
  624.   local_n_tshift = cdata->local_n_tshift;
  625.   local_linesz   = cdata->local_linesz;
  626.   local_symesz   = cdata->local_symesz;
  627.   local_auxesz   = cdata->local_auxesz;
  628.  
  629.   /* Allocate space for raw symbol and aux entries, based on their
  630.      space requirements as reported by BFD.  */
  631.   temp_sym = (char *) xmalloc
  632.      (cdata->local_symesz + cdata->local_auxesz);
  633.   temp_aux = temp_sym + cdata->local_symesz;
  634.   back_to = make_cleanup (free_current_contents, &temp_sym);
  635. /* End of warning */
  636.  
  637.   /* Read the line number table, all at once.  */
  638.   info->min_lineno_offset = 0;
  639.   info->max_lineno_offset = 0;
  640.   bfd_map_over_sections (abfd, find_linenos, (PTR) info);
  641.  
  642.   make_cleanup (free_linetab, 0);
  643.   val = init_lineno (desc, info->min_lineno_offset, 
  644.              info->max_lineno_offset - info->min_lineno_offset);
  645.   if (val < 0)
  646.     error ("\"%s\": error reading line numbers\n", name);
  647.  
  648.   /* Now read the string table, all at once.  */
  649.  
  650.   make_cleanup (free_stringtab, 0);
  651.   val = init_stringtab (desc, stringtab_offset);
  652.   if (val < 0)
  653.     error ("\"%s\": can't get string table", name);
  654.  
  655.   init_minimal_symbol_collection ();
  656.   make_cleanup (discard_minimal_symbols, 0);
  657.  
  658.   /* Now that the executable file is positioned at symbol table,
  659.      process it and define symbols accordingly.  */
  660.  
  661.   read_coff_symtab ((long)symtab_offset, num_symbols, objfile);
  662.  
  663.   /* Sort symbols alphabetically within each block.  */
  664.  
  665.   {
  666.     struct symtab *s;
  667.     for (s = objfile -> symtabs; s != NULL; s = s -> next)
  668.       {
  669.     sort_symtab_syms (s);
  670.       }
  671.   }
  672.  
  673.   /* Install any minimal symbols that have been collected as the current
  674.      minimal symbols for this objfile. */
  675.  
  676.   install_minimal_symbols (objfile);
  677.  
  678.   bfd_map_over_sections (abfd, coff_locate_sections, (PTR) info);
  679.  
  680.   if (info->stabsect)
  681.     {
  682.       /* FIXME: dubious.  Why can't we use something normal like
  683.      bfd_get_section_contents?  */
  684.       fseek ((GDB_FILE *) abfd->iostream, abfd->where, 0);
  685.  
  686.       stabsize = bfd_section_size (abfd, info->stabsect);
  687.       stabstrsize = bfd_section_size (abfd, info->stabstrsect);
  688.  
  689.       coffstab_build_psymtabs (objfile,
  690.                    section_offsets,
  691.                    mainline,
  692.                    info->stabsect->filepos, stabsize,
  693.                    info->stabstrsect->filepos, stabstrsize);
  694.     }
  695.  
  696.   do_cleanups (back_to);
  697. }
  698.  
  699. static void
  700. coff_new_init (ignore)
  701.      struct objfile *ignore;
  702. {
  703. }
  704.  
  705. /* Perform any local cleanups required when we are done with a particular
  706.    objfile.  I.E, we are in the process of discarding all symbol information
  707.    for an objfile, freeing up all memory held for it, and unlinking the
  708.    objfile struct from the global list of known objfiles. */
  709.  
  710. static void
  711. coff_symfile_finish (objfile)
  712.      struct objfile *objfile;
  713. {
  714.   if (objfile -> sym_private != NULL)
  715.     {
  716.       mfree (objfile -> md, objfile -> sym_private);
  717.     }
  718. }
  719.  
  720.  
  721. /* Given pointers to a symbol table in coff style exec file,
  722.    analyze them and create struct symtab's describing the symbols.
  723.    NSYMS is the number of symbols in the symbol table.
  724.    We read them one at a time using read_one_sym ().  */
  725.  
  726. static void
  727. read_coff_symtab (symtab_offset, nsyms, objfile)
  728.      long symtab_offset;
  729.      int nsyms;
  730.      struct objfile *objfile;
  731. {
  732.   GDB_FILE *stream; 
  733.   register struct context_stack *new;
  734.   struct coff_symbol coff_symbol;
  735.   register struct coff_symbol *cs = &coff_symbol;
  736.   static struct internal_syment main_sym;
  737.   static union internal_auxent main_aux;
  738.   struct coff_symbol fcn_cs_saved;
  739.   static struct internal_syment fcn_sym_saved;
  740.   static union internal_auxent fcn_aux_saved;
  741.   struct symtab *s;
  742.   
  743.   /* A .file is open.  */
  744.   int in_source_file = 0;
  745.   int next_file_symnum = -1;
  746.  
  747.   /* Name of the current file.  */
  748.   char *filestring = "";
  749.   int depth = 0;
  750.   int fcn_first_line = 0;
  751.   int fcn_last_line = 0;
  752.   int fcn_start_addr = 0;
  753.   long fcn_line_ptr = 0;
  754.   int val;
  755.  
  756.   stream = bfd_cache_lookup(objfile->obfd);
  757.   if (!stream)
  758.    perror_with_name(objfile->name);
  759.  
  760.   /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
  761.      it's hard to know I've really worked around it.  The fix should be
  762.      harmless, anyway).  The symptom of the bug is that the first
  763.      fread (in read_one_sym), will (in my example) actually get data
  764.      from file offset 268, when the fseek was to 264 (and ftell shows
  765.      264).  This causes all hell to break loose.  I was unable to
  766.      reproduce this on a short test program which operated on the same
  767.      file, performing (I think) the same sequence of operations.
  768.  
  769.      It stopped happening when I put in this rewind().
  770.  
  771.      FIXME: Find out if this has been reported to Sun, whether it has
  772.      been fixed in a later release, etc.  */
  773.  
  774.   rewind (stream);
  775.  
  776.   /* Position to read the symbol table. */
  777.   val = fseek (stream, (long)symtab_offset, 0);
  778.   if (val < 0)
  779.     perror_with_name (objfile->name);
  780.  
  781.   current_objfile = objfile;
  782.   nlist_stream_global = stream;
  783.   nlist_nsyms_global = nsyms;
  784.   last_source_file = NULL;
  785.   memset (opaque_type_chain, 0, sizeof opaque_type_chain);
  786.  
  787.   if (type_vector)            /* Get rid of previous one */
  788.     free ((PTR)type_vector);
  789.   type_vector_length = 160;
  790.   type_vector = (struct type **)
  791.         xmalloc (type_vector_length * sizeof (struct type *));
  792.   memset (type_vector, 0, type_vector_length * sizeof (struct type *));
  793.  
  794.   coff_start_symtab ();
  795.  
  796.   symnum = 0;
  797.   while (symnum < nsyms)
  798.     {
  799.       QUIT;            /* Make this command interruptable.  */
  800.       read_one_sym (cs, &main_sym, &main_aux);
  801.  
  802. #ifdef SEM
  803.       temp_sem_val = cs->c_name[0] << 24 | cs->c_name[1] << 16 |
  804.                      cs->c_name[2] << 8 | cs->c_name[3];
  805.       if (int_sem_val == temp_sem_val)
  806.         last_coffsem = (int) strtol (cs->c_name+4, (char **) NULL, 10);
  807. #endif
  808.  
  809.       if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
  810.     {
  811.       if (last_source_file)
  812.         coff_end_symtab (objfile);
  813.  
  814.       coff_start_symtab ();
  815.       complete_symtab ("_globals_", 0, 0);
  816.       /* done with all files, everything from here on out is globals */
  817.     }
  818.  
  819.       /* Special case for file with type declarations only, no text.  */
  820.       if (!last_source_file && SDB_TYPE (cs->c_type)
  821.       && cs->c_secnum == N_DEBUG)
  822.     complete_symtab (filestring, 0, 0);
  823.  
  824.       /* Typedefs should not be treated as symbol definitions.  */
  825.       if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
  826.     {
  827.       /* Record all functions -- external and static -- in minsyms. */
  828.       record_minimal_symbol (cs->c_name, cs->c_value, mst_text, objfile);
  829.  
  830.       fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
  831.       fcn_start_addr = cs->c_value;
  832.       fcn_cs_saved = *cs;
  833.       fcn_sym_saved = main_sym;
  834.       fcn_aux_saved = main_aux;
  835.       continue;
  836.     }
  837.  
  838.       switch (cs->c_sclass)
  839.     {
  840.       case C_EFCN:
  841.       case C_EXTDEF:
  842.       case C_ULABEL:
  843.       case C_USTATIC:
  844.       case C_LINE:
  845.       case C_ALIAS:
  846.       case C_HIDDEN:
  847.         complain (&bad_sclass_complaint, cs->c_name);
  848.         break;
  849.  
  850.       case C_FILE:
  851.         /*
  852.          * c_value field contains symnum of next .file entry in table
  853.          * or symnum of first global after last .file.
  854.          */
  855.         next_file_symnum = cs->c_value;
  856.         if (cs->c_naux > 0)
  857.           filestring = getfilename (&main_aux);
  858.         else
  859.           filestring = "";
  860.  
  861.         /*
  862.          * Complete symbol table for last object file
  863.          * containing debugging information.
  864.          */
  865.         if (last_source_file)
  866.           {
  867.         coff_end_symtab (objfile);
  868.         coff_start_symtab ();
  869.           }
  870.         in_source_file = 1;
  871.         break;
  872.  
  873.           case C_STAT:
  874.         if (cs->c_name[0] == '.') {
  875.             if (STREQ (cs->c_name, ".text")) {
  876.                 /* FIXME:  don't wire in ".text" as section name
  877.                        or symbol name! */
  878.                 /* Check for in_source_file deals with case of
  879.                    a file with debugging symbols
  880.                    followed by a later file with no symbols.  */
  881.                 if (in_source_file)
  882.                   complete_symtab (filestring, cs->c_value,
  883.                            main_aux.x_scn.x_scnlen);
  884.                 in_source_file = 0;
  885.             }
  886.             /* flush rest of '.' symbols */
  887.             break;
  888.         }
  889.         else if (!SDB_TYPE (cs->c_type)
  890.              && cs->c_name[0] == 'L'
  891.              && (strncmp (cs->c_name, "LI%", 3) == 0
  892.              || strncmp (cs->c_name, "LF%", 3) == 0
  893.              || strncmp (cs->c_name,"LC%",3) == 0
  894.              || strncmp (cs->c_name,"LP%",3) == 0
  895.              || strncmp (cs->c_name,"LPB%",4) == 0
  896.              || strncmp (cs->c_name,"LBB%",4) == 0
  897.              || strncmp (cs->c_name,"LBE%",4) == 0
  898.              || strncmp (cs->c_name,"LPBX%",5) == 0))
  899.           /* At least on a 3b1, gcc generates swbeg and string labels
  900.          that look like this.  Ignore them.  */
  901.           break;
  902.         /* fall in for static symbols that don't start with '.' */
  903.       case C_EXT:
  904.         /* Record external symbols in minsyms if we don't have debug
  905.            info for them.  FIXME, this is probably the wrong thing
  906.            to do.  Why don't we record them even if we do have
  907.            debug symbol info?  What really belongs in the minsyms
  908.            anyway?  Fred!??  */
  909.         if (!SDB_TYPE (cs->c_type)) {
  910.         /* FIXME: This is BOGUS Will Robinson! 
  911.          Coff should provide the SEC_CODE flag for executable sections,
  912.          then if we could look up sections by section number we
  913.            could see if the flags indicate SEC_CODE.  If so, then
  914.          record this symbol as a function in the minimal symbol table.
  915.         But why are absolute syms recorded as functions, anyway?  */
  916.             if (cs->c_secnum <= text_bfd_scnum+1) {/* text or abs */
  917.                 record_minimal_symbol (cs->c_name, cs->c_value,
  918.                            mst_text, objfile);
  919.                 break;
  920.             } else {
  921.                 record_minimal_symbol (cs->c_name, cs->c_value,
  922.                            mst_data, objfile);
  923.                 break;
  924.             }
  925.         }
  926.         process_coff_symbol (cs, &main_aux, objfile);
  927.         break;
  928.  
  929.       case C_FCN:
  930.         if (STREQ (cs->c_name, ".bf"))
  931.           {
  932.         within_function = 1;
  933.  
  934.         /* value contains address of first non-init type code */
  935.         /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
  936.                 contains line number of '{' } */
  937.         if (cs->c_naux != 1)
  938.           complain (&bf_no_aux_complaint, cs->c_symnum);
  939.         fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
  940.  
  941.         /* Might want to check that locals are 0 and
  942.            context_stack_depth is zero, and complain if not.  */
  943.  
  944.         depth = 0;
  945.         new = push_context (depth, fcn_start_addr);
  946.         fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
  947.         new->name = process_coff_symbol (&fcn_cs_saved,
  948.                          &fcn_aux_saved, objfile);
  949.           }
  950.         else if (STREQ (cs->c_name, ".ef"))
  951.           {
  952.         /* the value of .ef is the address of epilogue code;
  953.            not useful for gdb.  */
  954.         /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
  955.                 contains number of lines to '}' */
  956.         new = pop_context ();
  957.         /* Stack must be empty now.  */
  958.         if (context_stack_depth > 0 || new == NULL)
  959.           {
  960.             complain (&ef_complaint, cs->c_symnum);
  961.             within_function = 0;
  962.             break;
  963.           }
  964.         if (cs->c_naux != 1)
  965.           {
  966.             complain (&ef_no_aux_complaint, cs->c_symnum);
  967.             fcn_last_line = 0x7FFFFFFF;
  968.           }
  969.         else
  970.           {
  971.             fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
  972.           }
  973.         enter_linenos (fcn_line_ptr, fcn_first_line, fcn_last_line);
  974.  
  975.         finish_block (new->name, &local_symbols, new->old_blocks,
  976.                   new->start_addr,
  977. #if defined (FUNCTION_EPILOGUE_SIZE)
  978.                   /* This macro should be defined only on
  979.                  machines where the
  980.                  fcn_aux_saved.x_sym.x_misc.x_fsize
  981.                  field is always zero.
  982.                  So use the .bf record information that
  983.                  points to the epilogue and add the size
  984.                  of the epilogue.  */
  985.                   cs->c_value + FUNCTION_EPILOGUE_SIZE,
  986. #else
  987.                   fcn_cs_saved.c_value +
  988.                       fcn_aux_saved.x_sym.x_misc.x_fsize,
  989. #endif
  990.                   objfile
  991.                   );
  992.         within_function = 0;
  993.           }
  994.         break;
  995.  
  996.       case C_BLOCK:
  997.         if (STREQ (cs->c_name, ".bb"))
  998.           {
  999.         push_context (++depth, cs->c_value);
  1000.           }
  1001.         else if (STREQ (cs->c_name, ".eb"))
  1002.           {
  1003.         new = pop_context ();
  1004.         if (depth-- != new->depth)
  1005.           {
  1006.             complain (&eb_complaint, symnum);
  1007.             break;
  1008.           }
  1009.         if (local_symbols && context_stack_depth > 0)
  1010.           {
  1011.             /* Make a block for the local symbols within.  */
  1012.             finish_block (0, &local_symbols, new->old_blocks,
  1013.                   new->start_addr, cs->c_value, objfile);
  1014.           }
  1015.         /* Now pop locals of block just finished.  */
  1016.         local_symbols = new->locals;
  1017.           }
  1018.         break;
  1019.  
  1020.       default:
  1021.         process_coff_symbol (cs, &main_aux, objfile);
  1022.         break;
  1023.     }
  1024.     }
  1025.  
  1026.   if (last_source_file)
  1027.     coff_end_symtab (objfile);
  1028.  
  1029.   /* Patch up any opaque types (references to types that are not defined
  1030.      in the file where they are referenced, e.g. "struct foo *bar").  */
  1031.   ALL_OBJFILE_SYMTABS (objfile, s)
  1032.     patch_opaque_types (s);
  1033.  
  1034.   current_objfile = NULL;
  1035. }
  1036.  
  1037. /* Routines for reading headers and symbols from executable.  */
  1038.  
  1039. #ifdef FIXME
  1040. /* Move these XXXMAGIC symbol defns into BFD!  */
  1041.  
  1042. /* Read COFF file header, check magic number,
  1043.    and return number of symbols. */
  1044. read_file_hdr (chan, file_hdr)
  1045.     int chan;
  1046.     FILHDR *file_hdr;
  1047. {
  1048.   lseek (chan, 0L, 0);
  1049.   if (myread (chan, (char *)file_hdr, FILHSZ) < 0)
  1050.     return -1;
  1051.  
  1052.   switch (file_hdr->f_magic)
  1053.     {
  1054. #ifdef MC68MAGIC
  1055.     case MC68MAGIC:
  1056. #endif
  1057. #ifdef NS32GMAGIC
  1058.       case NS32GMAGIC:
  1059.       case NS32SMAGIC:
  1060. #endif
  1061. #ifdef I386MAGIC
  1062.     case I386MAGIC:
  1063. #endif
  1064. #ifdef CLIPPERMAGIC
  1065.     case CLIPPERMAGIC:
  1066. #endif
  1067. #if defined (MC68KWRMAGIC) \
  1068.   && (!defined (MC68MAGIC) || MC68KWRMAGIC != MC68MAGIC)
  1069.     case MC68KWRMAGIC:
  1070. #endif
  1071. #ifdef MC68KROMAGIC
  1072.     case MC68KROMAGIC:
  1073.     case MC68KPGMAGIC:
  1074. #endif
  1075. #ifdef MC88DGMAGIC
  1076.     case MC88DGMAGIC:
  1077. #endif      
  1078. #ifdef MC88MAGIC
  1079.     case MC88MAGIC:
  1080. #endif      
  1081. #ifdef I960ROMAGIC
  1082.     case I960ROMAGIC:        /* Intel 960 */
  1083. #endif
  1084. #ifdef I960RWMAGIC
  1085.     case I960RWMAGIC:        /* Intel 960 */
  1086. #endif
  1087.     return file_hdr->f_nsyms;
  1088.  
  1089.       default:
  1090. #ifdef BADMAG
  1091.     if (BADMAG(file_hdr))
  1092.       return -1;
  1093.     else
  1094.       return file_hdr->f_nsyms;
  1095. #else
  1096.     return -1;
  1097. #endif
  1098.     }
  1099. }
  1100. #endif
  1101.  
  1102. /* Read the next symbol, swap it, and return it in both internal_syment
  1103.    form, and coff_symbol form.  Also return its first auxent, if any,
  1104.    in internal_auxent form, and skip any other auxents.  */
  1105.  
  1106. static void
  1107. read_one_sym (cs, sym, aux)
  1108.     register struct coff_symbol *cs;
  1109.     register struct internal_syment *sym;
  1110.     register union internal_auxent *aux;
  1111. {
  1112.   int i;
  1113.  
  1114.   cs->c_symnum = symnum;
  1115.   fread (temp_sym, local_symesz, 1, nlist_stream_global);
  1116.   bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *)sym);
  1117.   cs->c_naux = sym->n_numaux & 0xff;
  1118.   if (cs->c_naux >= 1)
  1119.     {
  1120.     fread (temp_aux, local_auxesz, 1, nlist_stream_global);
  1121.     bfd_coff_swap_aux_in (symfile_bfd, temp_aux, sym->n_type, sym->n_sclass,
  1122.               (char *)aux);
  1123.     /* If more than one aux entry, read past it (only the first aux
  1124.        is important). */
  1125.     for (i = 1; i < cs->c_naux; i++)
  1126.       fread (temp_aux, local_auxesz, 1, nlist_stream_global);
  1127.     }
  1128.   cs->c_name = getsymname (sym);
  1129.   cs->c_value = sym->n_value;
  1130.   cs->c_sclass = (sym->n_sclass & 0xff);
  1131.   cs->c_secnum = sym->n_scnum;
  1132.   cs->c_type = (unsigned) sym->n_type;
  1133.   if (!SDB_TYPE (cs->c_type))
  1134.     cs->c_type = 0;
  1135.  
  1136.   symnum += 1 + cs->c_naux;
  1137. }
  1138.  
  1139. /* Support for string table handling */
  1140.  
  1141. static char *stringtab = NULL;
  1142.  
  1143. static int
  1144. init_stringtab (chan, offset)
  1145.     int chan;
  1146.     long offset;
  1147. {
  1148.   long length;
  1149.   int val;
  1150.   unsigned char lengthbuf[4];
  1151.  
  1152.   free_stringtab ();
  1153.  
  1154.   if (lseek (chan, offset, 0) < 0)
  1155.     return -1;
  1156.  
  1157.   val = myread (chan, (char *)lengthbuf, sizeof lengthbuf);
  1158.   length = bfd_h_get_32 (symfile_bfd, lengthbuf);
  1159.  
  1160.   /* If no string table is needed, then the file may end immediately
  1161.      after the symbols.  Just return with `stringtab' set to null. */
  1162.   if (val != sizeof lengthbuf || length < sizeof lengthbuf)
  1163.     return 0;
  1164.  
  1165.   stringtab = (char *) xmalloc (length);
  1166.   memcpy (stringtab, &length, sizeof length);
  1167.   if (length == sizeof length)        /* Empty table -- just the count */
  1168.     return 0;
  1169.  
  1170.   val = myread (chan, stringtab + sizeof lengthbuf, length - sizeof lengthbuf);
  1171.   if (val != length - sizeof lengthbuf || stringtab[length - 1] != '\0')
  1172.     return -1;
  1173.  
  1174.   return 0;
  1175. }
  1176.  
  1177. static void
  1178. free_stringtab ()
  1179. {
  1180.   if (stringtab)
  1181.     free (stringtab);
  1182.   stringtab = NULL;
  1183. }
  1184.  
  1185. static char *
  1186. getsymname (symbol_entry)
  1187.     struct internal_syment *symbol_entry;
  1188. {
  1189.   static char buffer[SYMNMLEN+1];
  1190.   char *result;
  1191.  
  1192.   if (symbol_entry->_n._n_n._n_zeroes == 0)
  1193.     {
  1194.       result = stringtab + symbol_entry->_n._n_n._n_offset;
  1195.     }
  1196.   else
  1197.     {
  1198.       strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
  1199.       buffer[SYMNMLEN] = '\0';
  1200.       result = buffer;
  1201.     }
  1202.   return result;
  1203. }
  1204.  
  1205. /* Extract the file name from the aux entry of a C_FILE symbol.  Return
  1206.    only the last component of the name.  Result is in static storage and
  1207.    is only good for temporary use.  */
  1208.  
  1209. static char *
  1210. getfilename (aux_entry)
  1211.     union internal_auxent *aux_entry;
  1212. {
  1213.   static char buffer[BUFSIZ];
  1214.   register char *temp;
  1215.   char *result;
  1216.  
  1217.   if (aux_entry->x_file.x_n.x_zeroes == 0)
  1218.     strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
  1219.   else
  1220.     {
  1221.       strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
  1222.       buffer[FILNMLEN] = '\0';
  1223.     }
  1224.   result = buffer;
  1225.   if ((temp = strrchr (result, '/')) != NULL)
  1226.     result = temp + 1;
  1227.   return (result);
  1228. }
  1229.  
  1230. /* Support for line number handling */
  1231. static char *linetab = NULL;
  1232. static long linetab_offset;
  1233. static unsigned long linetab_size;
  1234.  
  1235. /* Read in all the line numbers for fast lookups later.  Leave them in
  1236.    external (unswapped) format in memory; we'll swap them as we enter
  1237.    them into GDB's data structures.  */
  1238.  
  1239. static int
  1240. init_lineno (chan, offset, size)
  1241.     int chan;
  1242.     long offset;
  1243.     int size;
  1244. {
  1245.   int val;
  1246.  
  1247.   linetab_offset = offset;
  1248.   linetab_size = size;
  1249.  
  1250.   free_linetab();
  1251.  
  1252.   if (size == 0)
  1253.     return 0;
  1254.  
  1255.   if (lseek (chan, offset, 0) < 0)
  1256.     return -1;
  1257.   
  1258.   /* Allocate the desired table, plus a sentinel */
  1259.   linetab = (char *) xmalloc (size + local_linesz);
  1260.  
  1261.   val = myread (chan, linetab, size);
  1262.   if (val != size)
  1263.     return -1;
  1264.  
  1265.   /* Terminate it with an all-zero sentinel record */
  1266.   memset (linetab + size, 0, local_linesz);
  1267.  
  1268.   return 0;
  1269. }
  1270.  
  1271. static void
  1272. free_linetab ()
  1273. {
  1274.   if (linetab)
  1275.     free (linetab);
  1276.   linetab = NULL;
  1277. }
  1278.  
  1279. #if !defined (L_LNNO32)
  1280. #define L_LNNO32(lp) ((lp)->l_lnno)
  1281. #endif
  1282.  
  1283. static void
  1284. enter_linenos (file_offset, first_line, last_line)
  1285.     long file_offset;
  1286.     register int first_line;
  1287.     register int last_line;
  1288. {
  1289.   register char *rawptr;
  1290.   struct internal_lineno lptr;
  1291.  
  1292.   if (file_offset < linetab_offset)
  1293.     {
  1294.       complain (&lineno_complaint, file_offset);
  1295.       if (file_offset > linetab_size)    /* Too big to be an offset? */
  1296.     return;
  1297.       file_offset += linetab_offset;  /* Try reading at that linetab offset */
  1298.     }
  1299.   
  1300.   rawptr = &linetab[file_offset - linetab_offset];
  1301.  
  1302.   /* skip first line entry for each function */
  1303.   rawptr += local_linesz;
  1304.   /* line numbers start at one for the first line of the function */
  1305.   first_line--;
  1306.  
  1307.   for (;;) {
  1308.     bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
  1309.     rawptr += local_linesz;
  1310.     /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
  1311.     if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
  1312.       coff_record_line (first_line + L_LNNO32 (&lptr), lptr.l_addr.l_paddr);
  1313.     else
  1314.       break;
  1315.   } 
  1316. }
  1317.  
  1318. static void
  1319. patch_type (type, real_type)
  1320.     struct type *type;
  1321.     struct type *real_type;
  1322. {
  1323.   register struct type *target = TYPE_TARGET_TYPE (type);
  1324.   register struct type *real_target = TYPE_TARGET_TYPE (real_type);
  1325.   int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
  1326.  
  1327.   TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
  1328.   TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
  1329.   TYPE_FIELDS (target) = (struct field *) TYPE_ALLOC (target, field_size);
  1330.  
  1331.   memcpy (TYPE_FIELDS (target), TYPE_FIELDS (real_target), field_size);
  1332.  
  1333.   if (TYPE_NAME (real_target))
  1334.     {
  1335.       if (TYPE_NAME (target))
  1336.     free (TYPE_NAME (target));
  1337.       TYPE_NAME (target) = concat (TYPE_NAME (real_target), NULL);
  1338.     }
  1339. }
  1340.  
  1341. /* Patch up all appropriate typedef symbols in the opaque_type_chains
  1342.    so that they can be used to print out opaque data structures properly.  */
  1343.  
  1344. static void
  1345. patch_opaque_types (s)
  1346.      struct symtab *s;
  1347. {
  1348.   register struct block *b;
  1349.   register int i;
  1350.   register struct symbol *real_sym;
  1351.   
  1352.   /* Go through the per-file symbols only */
  1353.   b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
  1354.   for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
  1355.     {
  1356.       /* Find completed typedefs to use to fix opaque ones.
  1357.      Remove syms from the chain when their types are stored,
  1358.      but search the whole chain, as there may be several syms
  1359.      from different files with the same name.  */
  1360.       real_sym = BLOCK_SYM (b, i);
  1361.       if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
  1362.       SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
  1363.       TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
  1364.       TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
  1365.     {
  1366.       register char *name = SYMBOL_NAME (real_sym);
  1367.       register int hash = hashname (name);
  1368.       register struct symbol *sym, *prev;
  1369.       
  1370.       prev = 0;
  1371.       for (sym = opaque_type_chain[hash]; sym;)
  1372.         {
  1373.           if (name[0] == SYMBOL_NAME (sym)[0] &&
  1374.           STREQ (name + 1, SYMBOL_NAME (sym) + 1))
  1375.         {
  1376.           if (prev)
  1377.             {
  1378.               SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
  1379.             }
  1380.           else
  1381.             {
  1382.               opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
  1383.             }
  1384.           
  1385.           patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
  1386.           
  1387.           if (prev)
  1388.             {
  1389.               sym = SYMBOL_VALUE_CHAIN (prev);
  1390.             }
  1391.           else
  1392.             {
  1393.               sym = opaque_type_chain[hash];
  1394.             }
  1395.         }
  1396.           else
  1397.         {
  1398.           prev = sym;
  1399.           sym = SYMBOL_VALUE_CHAIN (sym);
  1400.         }
  1401.         }
  1402.     }
  1403.     }
  1404. }
  1405.  
  1406. static struct symbol *
  1407. process_coff_symbol (cs, aux, objfile)
  1408.      register struct coff_symbol *cs;
  1409.      register union internal_auxent *aux;
  1410.      struct objfile *objfile;
  1411. {
  1412.   register struct symbol *sym
  1413.     = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
  1414.                        sizeof (struct symbol));
  1415.   char *name;
  1416.   struct type *temptype;
  1417.  
  1418.   memset (sym, 0, sizeof (struct symbol));
  1419.   name = cs->c_name;
  1420.   name = EXTERNAL_NAME (name, objfile->obfd);
  1421.   SYMBOL_NAME (sym) = obstack_copy0 (&objfile->symbol_obstack, name,
  1422.                      strlen (name));
  1423.  
  1424.   /* default assumptions */
  1425.   SYMBOL_VALUE (sym) = cs->c_value;
  1426.   SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1427.  
  1428.   if (ISFCN (cs->c_type))
  1429.     {
  1430. #if 0
  1431.        /* FIXME:  This has NOT been tested.  The DBX version has.. */
  1432.        /* Generate a template for the type of this function.  The 
  1433.       types of the arguments will be added as we read the symbol 
  1434.       table. */
  1435.        struct type *new = (struct type *)
  1436.             obstack_alloc (&objfile->symbol_obstack, sizeof (struct type));
  1437.        
  1438.        memcpy (new, lookup_function_type (decode_function_type (cs, cs->c_type, aux)),
  1439.               sizeof(struct type));
  1440.        SYMBOL_TYPE (sym) = new;
  1441.        in_function_type = SYMBOL_TYPE(sym);
  1442. #else
  1443.        SYMBOL_TYPE(sym) = 
  1444.      lookup_function_type (decode_function_type (cs, cs->c_type, aux));
  1445. #endif
  1446.  
  1447.       SYMBOL_CLASS (sym) = LOC_BLOCK;
  1448.       if (cs->c_sclass == C_STAT)
  1449.     add_symbol_to_list (sym, &file_symbols);
  1450.       else if (cs->c_sclass == C_EXT)
  1451.     add_symbol_to_list (sym, &global_symbols);
  1452.     }
  1453.   else
  1454.     {
  1455.       SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux);
  1456.       switch (cs->c_sclass)
  1457.     {
  1458.       case C_NULL:
  1459.         break;
  1460.  
  1461.       case C_AUTO:
  1462.         SYMBOL_CLASS (sym) = LOC_LOCAL;
  1463.         add_symbol_to_list (sym, &local_symbols);
  1464.         break;
  1465.  
  1466.       case C_EXT:
  1467.         SYMBOL_CLASS (sym) = LOC_STATIC;
  1468.         SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
  1469.         add_symbol_to_list (sym, &global_symbols);
  1470.         break;
  1471.  
  1472.       case C_STAT:
  1473.         SYMBOL_CLASS (sym) = LOC_STATIC;
  1474.         SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
  1475.         if (within_function) {
  1476.           /* Static symbol of local scope */
  1477.           add_symbol_to_list (sym, &local_symbols);
  1478.         }
  1479.         else {
  1480.           /* Static symbol at top level of file */
  1481.           add_symbol_to_list (sym, &file_symbols);
  1482.         }
  1483.         break;
  1484.  
  1485. #ifdef C_GLBLREG        /* AMD coff */
  1486.       case C_GLBLREG:
  1487. #endif
  1488.       case C_REG:
  1489.         SYMBOL_CLASS (sym) = LOC_REGISTER;
  1490.         SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM(cs->c_value);
  1491.         add_symbol_to_list (sym, &local_symbols);
  1492.         break;
  1493.  
  1494.       case C_LABEL:
  1495.         break;
  1496.  
  1497.       case C_ARG:
  1498.         SYMBOL_CLASS (sym) = LOC_ARG;
  1499. #if 0
  1500.         /* FIXME:  This has not been tested. */
  1501.         /* Add parameter to function.  */
  1502.         add_param_to_type(&in_function_type,sym);
  1503. #endif
  1504.         add_symbol_to_list (sym, &local_symbols);
  1505. #if !defined (BELIEVE_PCC_PROMOTION) && (TARGET_BYTE_ORDER == BIG_ENDIAN)
  1506.         /* If PCC says a parameter is a short or a char,
  1507.            aligned on an int boundary, realign it to the "little end"
  1508.            of the int.  */
  1509.         temptype = lookup_fundamental_type (current_objfile, FT_INTEGER);
  1510.         if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
  1511.         && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT
  1512.         && 0 == SYMBOL_VALUE (sym) % TYPE_LENGTH (temptype))
  1513.         {
  1514.             SYMBOL_VALUE (sym) += TYPE_LENGTH (temptype)
  1515.                         - TYPE_LENGTH (SYMBOL_TYPE (sym));
  1516.         }
  1517. #endif
  1518.         break;
  1519.  
  1520.       case C_REGPARM:
  1521.         SYMBOL_CLASS (sym) = LOC_REGPARM;
  1522.         SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM(cs->c_value);
  1523.         add_symbol_to_list (sym, &local_symbols);
  1524. #if !defined (BELIEVE_PCC_PROMOTION)
  1525.     /* FIXME:  This should retain the current type, since it's just
  1526.        a register value.  gnu@adobe, 26Feb93 */
  1527.         /* If PCC says a parameter is a short or a char,
  1528.            it is really an int.  */
  1529.         temptype = lookup_fundamental_type (current_objfile, FT_INTEGER);
  1530.         if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
  1531.         && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT)
  1532.         {
  1533.             SYMBOL_TYPE (sym) = TYPE_UNSIGNED (SYMBOL_TYPE (sym))
  1534.             ? lookup_fundamental_type (current_objfile,
  1535.                            FT_UNSIGNED_INTEGER)
  1536.                 : temptype;
  1537.         }
  1538. #endif
  1539.         break;
  1540.         
  1541.       case C_TPDEF:
  1542.         SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1543.         SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1544.  
  1545.         /* If type has no name, give it one */
  1546.         if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
  1547.           TYPE_NAME (SYMBOL_TYPE (sym)) = concat (SYMBOL_NAME (sym), NULL);
  1548.  
  1549.         /* Keep track of any type which points to empty structured type,
  1550.         so it can be filled from a definition from another file.  A
  1551.         simple forward reference (TYPE_CODE_UNDEF) is not an
  1552.         empty structured type, though; the forward references
  1553.         work themselves out via the magic of coff_lookup_type.  */
  1554.         if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR &&
  1555.         TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0 &&
  1556.         TYPE_CODE   (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) !=
  1557.                         TYPE_CODE_UNDEF)
  1558.           {
  1559.         register int i = hashname (SYMBOL_NAME (sym));
  1560.  
  1561.         SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
  1562.         opaque_type_chain[i] = sym;
  1563.           }
  1564.         add_symbol_to_list (sym, &file_symbols);
  1565.         break;
  1566.  
  1567.       case C_STRTAG:
  1568.       case C_UNTAG:
  1569.       case C_ENTAG:
  1570.         SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1571.         SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
  1572.  
  1573.             /* Some compilers try to be helpful by inventing "fake"
  1574.                names for anonymous enums, structures, and unions, like
  1575.                "~0fake" or ".0fake".  Thanks, but no thanks... */
  1576.         if (TYPE_TAG_NAME (SYMBOL_TYPE (sym)) == 0)
  1577.           if (SYMBOL_NAME(sym) != NULL
  1578.           && *SYMBOL_NAME(sym) != '~'
  1579.           && *SYMBOL_NAME(sym) != '.')
  1580.         TYPE_TAG_NAME (SYMBOL_TYPE (sym)) =
  1581.           concat (SYMBOL_NAME (sym), NULL);
  1582.  
  1583.         add_symbol_to_list (sym, &file_symbols);
  1584.         break;
  1585.  
  1586.       default:
  1587.         break;
  1588.     }
  1589.     }
  1590.   return sym;
  1591. }
  1592.  
  1593. /* Decode a coff type specifier;
  1594.    return the type that is meant.  */
  1595.  
  1596. static
  1597. struct type *
  1598. decode_type (cs, c_type, aux)
  1599.      register struct coff_symbol *cs;
  1600.      unsigned int c_type;
  1601.      register union internal_auxent *aux;
  1602. {
  1603.   register struct type *type = 0;
  1604.   unsigned int new_c_type;
  1605.  
  1606.   if (c_type & ~N_BTMASK)
  1607.     {
  1608.       new_c_type = DECREF (c_type);
  1609.       if (ISPTR (c_type))
  1610.     {
  1611.       type = decode_type (cs, new_c_type, aux);
  1612.       type = lookup_pointer_type (type);
  1613.     }
  1614.       else if (ISFCN (c_type))
  1615.     {
  1616.       type = decode_type (cs, new_c_type, aux);
  1617.       type = lookup_function_type (type);
  1618.     }
  1619.       else if (ISARY (c_type))
  1620.     {
  1621.       int i, n;
  1622.       register unsigned short *dim;
  1623.       struct type *base_type, *index_type, *range_type;
  1624.  
  1625.       /* Define an array type.  */
  1626.       /* auxent refers to array, not base type */
  1627.       if (aux->x_sym.x_tagndx.l == 0)
  1628.         cs->c_naux = 0;
  1629.  
  1630.       /* shift the indices down */
  1631.       dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
  1632.       i = 1;
  1633.       n = dim[0];
  1634.       for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
  1635.         *dim = *(dim + 1);
  1636.       *dim = 0;
  1637.  
  1638.       base_type = decode_type (cs, new_c_type, aux);
  1639.       index_type = lookup_fundamental_type (current_objfile, FT_INTEGER);
  1640.       range_type =
  1641.         create_range_type ((struct type *) NULL, index_type, 0, n - 1);
  1642.       type =
  1643.         create_array_type ((struct type *) NULL, base_type, range_type);
  1644.     }
  1645.       return type;
  1646.     }
  1647.  
  1648.   /* Reference to existing type.  This only occurs with the
  1649.      struct, union, and enum types.  EPI a29k coff
  1650.      fakes us out by producing aux entries with a nonzero
  1651.      x_tagndx for definitions of structs, unions, and enums, so we
  1652.      have to check the c_sclass field.  SCO 3.2v4 cc gets confused
  1653.      with pointers to pointers to defined structs, and generates
  1654.      negative x_tagndx fields.  */
  1655.   if (cs->c_naux > 0 && aux->x_sym.x_tagndx.l != 0)
  1656.     {
  1657.       if (cs->c_sclass != C_STRTAG
  1658.       && cs->c_sclass != C_UNTAG
  1659.       && cs->c_sclass != C_ENTAG
  1660.       && aux->x_sym.x_tagndx.l >= 0)
  1661.     {
  1662.       type = coff_alloc_type (aux->x_sym.x_tagndx.l);
  1663.       return type;
  1664.     } else {
  1665.       complain (&tagndx_bad_complaint, cs->c_name);
  1666.       /* And fall through to decode_base_type... */
  1667.     }
  1668.     }
  1669.  
  1670.   return decode_base_type (cs, BTYPE (c_type), aux);
  1671. }
  1672.  
  1673. /* Decode a coff type specifier for function definition;
  1674.    return the type that the function returns.  */
  1675.  
  1676. static
  1677. struct type *
  1678. decode_function_type (cs, c_type, aux)
  1679.      register struct coff_symbol *cs;
  1680.      unsigned int c_type;
  1681.      register union internal_auxent *aux;
  1682. {
  1683.   if (aux->x_sym.x_tagndx.l == 0)
  1684.     cs->c_naux = 0;    /* auxent refers to function, not base type */
  1685.  
  1686.   return decode_type (cs, DECREF (c_type), aux);
  1687. }
  1688.  
  1689. /* basic C types */
  1690.  
  1691. static
  1692. struct type *
  1693. decode_base_type (cs, c_type, aux)
  1694.      register struct coff_symbol *cs;
  1695.      unsigned int c_type;
  1696.      register union internal_auxent *aux;
  1697. {
  1698.   struct type *type;
  1699.  
  1700.   switch (c_type)
  1701.     {
  1702.       case T_NULL:
  1703.         /* shows up with "void (*foo)();" structure members */
  1704.     return lookup_fundamental_type (current_objfile, FT_VOID);
  1705.  
  1706. #if 0
  1707. /* DGUX actually defines both T_ARG and T_VOID to the same value.  */
  1708. #ifdef T_ARG
  1709.       case T_ARG:
  1710.     /* Shows up in DGUX, I think.  Not sure where.  */
  1711.     return lookup_fundamental_type (current_objfile, FT_VOID);    /* shouldn't show up here */
  1712. #endif
  1713. #endif /* 0 */
  1714.  
  1715. #ifdef T_VOID
  1716.       case T_VOID:
  1717.     /* Intel 960 COFF has this symbol and meaning.  */
  1718.     return lookup_fundamental_type (current_objfile, FT_VOID);
  1719. #endif
  1720.  
  1721.       case T_CHAR:
  1722.     return lookup_fundamental_type (current_objfile, FT_CHAR);
  1723.  
  1724.       case T_SHORT:
  1725.     return lookup_fundamental_type (current_objfile, FT_SHORT);
  1726.  
  1727.       case T_INT:
  1728.     return lookup_fundamental_type (current_objfile, FT_INTEGER);
  1729.  
  1730.       case T_LONG:
  1731.     return lookup_fundamental_type (current_objfile, FT_LONG);
  1732.  
  1733.       case T_FLOAT:
  1734.     return lookup_fundamental_type (current_objfile, FT_FLOAT);
  1735.  
  1736.       case T_DOUBLE:
  1737.     return lookup_fundamental_type (current_objfile, FT_DBL_PREC_FLOAT);
  1738.  
  1739.       case T_STRUCT:
  1740.     if (cs->c_naux != 1)
  1741.       {
  1742.         /* anonymous structure type */
  1743.         type = coff_alloc_type (cs->c_symnum);
  1744.         TYPE_CODE (type) = TYPE_CODE_STRUCT;
  1745.         TYPE_NAME (type) = NULL;
  1746.         /* This used to set the tag to "<opaque>".  But I think setting it
  1747.            to NULL is right, and the printing code can print it as
  1748.            "struct {...}".  */
  1749.         TYPE_TAG_NAME (type) = NULL;
  1750.         INIT_CPLUS_SPECIFIC(type);
  1751.         TYPE_LENGTH (type) = 0;
  1752.         TYPE_FIELDS (type) = 0;
  1753.         TYPE_NFIELDS (type) = 0;
  1754.       }
  1755.     else
  1756.       {
  1757.         type = coff_read_struct_type (cs->c_symnum,
  1758.                     aux->x_sym.x_misc.x_lnsz.x_size,
  1759.                     aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
  1760.       }
  1761.     return type;
  1762.  
  1763.       case T_UNION:
  1764.     if (cs->c_naux != 1)
  1765.       {
  1766.         /* anonymous union type */
  1767.         type = coff_alloc_type (cs->c_symnum);
  1768.         TYPE_NAME (type) = NULL;
  1769.         /* This used to set the tag to "<opaque>".  But I think setting it
  1770.            to NULL is right, and the printing code can print it as
  1771.            "union {...}".  */
  1772.         TYPE_TAG_NAME (type) = NULL;
  1773.         INIT_CPLUS_SPECIFIC(type);
  1774.         TYPE_LENGTH (type) = 0;
  1775.         TYPE_FIELDS (type) = 0;
  1776.         TYPE_NFIELDS (type) = 0;
  1777.       }
  1778.     else
  1779.       {
  1780.         type = coff_read_struct_type (cs->c_symnum,
  1781.                     aux->x_sym.x_misc.x_lnsz.x_size,
  1782.                     aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
  1783.       }
  1784.     TYPE_CODE (type) = TYPE_CODE_UNION;
  1785.     return type;
  1786.  
  1787.       case T_ENUM:
  1788.     if (cs->c_naux != 1)
  1789.       {
  1790.         /* anonymous enum type */
  1791.         type = coff_alloc_type (cs->c_symnum);
  1792.         TYPE_CODE (type) = TYPE_CODE_ENUM;
  1793.         TYPE_NAME (type) = NULL;
  1794.         /* This used to set the tag to "<opaque>".  But I think setting it
  1795.            to NULL is right, and the printing code can print it as
  1796.            "enum {...}".  */
  1797.         TYPE_TAG_NAME (type) = NULL;
  1798.         TYPE_LENGTH (type) = 0;
  1799.         TYPE_FIELDS (type) = 0;
  1800.         TYPE_NFIELDS(type) = 0;
  1801.       }
  1802.     else
  1803.       {
  1804.         type = coff_read_enum_type (cs->c_symnum,
  1805.                     aux->x_sym.x_misc.x_lnsz.x_size,
  1806.                     aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
  1807.       }
  1808.     return type;
  1809.  
  1810.       case T_MOE:
  1811.     /* shouldn't show up here */
  1812.     break;
  1813.  
  1814.       case T_UCHAR:
  1815.     return lookup_fundamental_type (current_objfile, FT_UNSIGNED_CHAR);
  1816.  
  1817.       case T_USHORT:
  1818.     return lookup_fundamental_type (current_objfile, FT_UNSIGNED_SHORT);
  1819.  
  1820.       case T_UINT:
  1821.     return lookup_fundamental_type (current_objfile, FT_UNSIGNED_INTEGER);
  1822.  
  1823.       case T_ULONG:
  1824.     return lookup_fundamental_type (current_objfile, FT_UNSIGNED_LONG);
  1825.     }
  1826.   complain (&unexpected_type_complaint, cs->c_name);
  1827.   return lookup_fundamental_type (current_objfile, FT_VOID);
  1828. }
  1829.  
  1830. /* This page contains subroutines of read_type.  */
  1831.  
  1832. /* Read the description of a structure (or union type)
  1833.    and return an object describing the type.  */
  1834.  
  1835. static struct type *
  1836. coff_read_struct_type (index, length, lastsym)
  1837.      int index;
  1838.      int length;
  1839.      int lastsym;
  1840. {
  1841.   struct nextfield
  1842.     {
  1843.       struct nextfield *next;
  1844.       struct field field;
  1845.     };
  1846.  
  1847.   register struct type *type;
  1848.   register struct nextfield *list = 0;
  1849.   struct nextfield *new;
  1850.   int nfields = 0;
  1851.   register int n;
  1852.   char *name;
  1853.   struct coff_symbol member_sym;
  1854.   register struct coff_symbol *ms = &member_sym;
  1855.   struct internal_syment sub_sym;
  1856.   union internal_auxent sub_aux;
  1857.   int done = 0;
  1858.  
  1859.   type = coff_alloc_type (index);
  1860.   TYPE_CODE (type) = TYPE_CODE_STRUCT;
  1861.   INIT_CPLUS_SPECIFIC(type);
  1862.   TYPE_LENGTH (type) = length;
  1863.  
  1864.   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
  1865.     {
  1866.       read_one_sym (ms, &sub_sym, &sub_aux);
  1867.       name = ms->c_name;
  1868.       name = EXTERNAL_NAME (name, current_objfile->obfd);
  1869.  
  1870.       switch (ms->c_sclass)
  1871.     {
  1872.       case C_MOS:
  1873.       case C_MOU:
  1874.  
  1875.         /* Get space to record the next field's data.  */
  1876.         new = (struct nextfield *) alloca (sizeof (struct nextfield));
  1877.         new->next = list;
  1878.         list = new;
  1879.  
  1880.         /* Save the data.  */
  1881.         list->field.name = savestring (name, strlen (name));
  1882.         list->field.type = decode_type (ms, ms->c_type, &sub_aux);
  1883.         list->field.bitpos = 8 * ms->c_value;
  1884.         list->field.bitsize = 0;
  1885.         nfields++;
  1886.         break;
  1887.  
  1888.       case C_FIELD:
  1889.  
  1890.         /* Get space to record the next field's data.  */
  1891.         new = (struct nextfield *) alloca (sizeof (struct nextfield));
  1892.         new->next = list;
  1893.         list = new;
  1894.  
  1895.         /* Save the data.  */
  1896.         list->field.name = savestring (name, strlen (name));
  1897.         list->field.type = decode_type (ms, ms->c_type, &sub_aux);
  1898.         list->field.bitpos = ms->c_value;
  1899.         list->field.bitsize = sub_aux.x_sym.x_misc.x_lnsz.x_size;
  1900.         nfields++;
  1901.         break;
  1902.  
  1903.       case C_EOS:
  1904.         done = 1;
  1905.         break;
  1906.     }
  1907.     }
  1908.   /* Now create the vector of fields, and record how big it is.  */
  1909.  
  1910.   TYPE_NFIELDS (type) = nfields;
  1911.   TYPE_FIELDS (type) = (struct field *)
  1912.     TYPE_ALLOC (type, sizeof (struct field) * nfields);
  1913.  
  1914.   /* Copy the saved-up fields into the field vector.  */
  1915.  
  1916.   for (n = nfields; list; list = list->next)
  1917.     TYPE_FIELD (type, --n) = list->field;
  1918.  
  1919.   return type;
  1920. }
  1921.  
  1922. /* Read a definition of an enumeration type,
  1923.    and create and return a suitable type object.
  1924.    Also defines the symbols that represent the values of the type.  */
  1925.  
  1926. /* ARGSUSED */
  1927. static struct type *
  1928. coff_read_enum_type (index, length, lastsym)
  1929.      int index;
  1930.      int length;
  1931.      int lastsym;
  1932. {
  1933.   register struct symbol *sym;
  1934.   register struct type *type;
  1935.   int nsyms = 0;
  1936.   int done = 0;
  1937.   struct pending **symlist;
  1938.   struct coff_symbol member_sym;
  1939.   register struct coff_symbol *ms = &member_sym;
  1940.   struct internal_syment sub_sym;
  1941.   union internal_auxent sub_aux;
  1942.   struct pending *osyms, *syms;
  1943.   int o_nsyms;
  1944.   register int n;
  1945.   char *name;
  1946.  
  1947.   type = coff_alloc_type (index);
  1948.   if (within_function)
  1949.     symlist = &local_symbols;
  1950.   else
  1951.     symlist = &file_symbols;
  1952.   osyms = *symlist;
  1953.   o_nsyms = osyms ? osyms->nsyms : 0;
  1954.  
  1955.   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
  1956.     {
  1957.       read_one_sym (ms, &sub_sym, &sub_aux);
  1958.       name = ms->c_name;
  1959.       name = EXTERNAL_NAME (name, current_objfile->obfd);
  1960.  
  1961.       switch (ms->c_sclass)
  1962.     {
  1963.       case C_MOE:
  1964.         sym = (struct symbol *) xmalloc (sizeof (struct symbol));
  1965.         memset (sym, 0, sizeof (struct symbol));
  1966.  
  1967.         SYMBOL_NAME (sym) = savestring (name, strlen (name));
  1968.         SYMBOL_CLASS (sym) = LOC_CONST;
  1969.         SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1970.         SYMBOL_VALUE (sym) = ms->c_value;
  1971.         add_symbol_to_list (sym, symlist);
  1972.         nsyms++;
  1973.         break;
  1974.  
  1975.       case C_EOS:
  1976.         /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
  1977.            up the count of how many symbols to read.  So stop
  1978.            on .eos.  */
  1979.         done = 1;
  1980.         break;
  1981.     }
  1982.     }
  1983.  
  1984.   /* Now fill in the fields of the type-structure.  */
  1985.  
  1986.   if (length > 0)
  1987.     TYPE_LENGTH (type) =  length;
  1988.   else
  1989.     TYPE_LENGTH (type) =  TARGET_INT_BIT / TARGET_CHAR_BIT;    /* Assume ints */
  1990.   TYPE_CODE (type) = TYPE_CODE_ENUM;
  1991.   TYPE_NFIELDS (type) = nsyms;
  1992.   TYPE_FIELDS (type) = (struct field *)
  1993.     TYPE_ALLOC (type, sizeof (struct field) * nsyms);
  1994.  
  1995.   /* Find the symbols for the values and put them into the type.
  1996.      The symbols can be found in the symlist that we put them on
  1997.      to cause them to be defined.  osyms contains the old value
  1998.      of that symlist; everything up to there was defined by us.  */
  1999.   /* Note that we preserve the order of the enum constants, so
  2000.      that in something like "enum {FOO, LAST_THING=FOO}" we print
  2001.      FOO, not LAST_THING.  */
  2002.  
  2003.   for (syms = *symlist, n = 0; syms; syms = syms->next)
  2004.     {
  2005.       int j = 0;
  2006.       if (syms == osyms)
  2007.     j = o_nsyms;
  2008.       for (; j < syms->nsyms; j++,n++)
  2009.     {
  2010.       struct symbol *xsym = syms->symbol[j];
  2011.       SYMBOL_TYPE (xsym) = type;
  2012.       TYPE_FIELD_NAME (type, n) = SYMBOL_NAME (xsym);
  2013.       TYPE_FIELD_VALUE (type, n) = 0;
  2014.       TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (xsym);
  2015.       TYPE_FIELD_BITSIZE (type, n) = 0;
  2016.     }
  2017.       if (syms == osyms)
  2018.     break;
  2019.     }
  2020.  
  2021. #if 0
  2022.   /* This screws up perfectly good C programs with enums.  FIXME.  */
  2023.   /* Is this Modula-2's BOOLEAN type?  Flag it as such if so. */
  2024.   if(TYPE_NFIELDS(type) == 2 &&
  2025.      ((STREQ(TYPE_FIELD_NAME(type,0),"TRUE") &&
  2026.        STREQ(TYPE_FIELD_NAME(type,1),"FALSE")) ||
  2027.       (STREQ(TYPE_FIELD_NAME(type,1),"TRUE") &&
  2028.        STREQ(TYPE_FIELD_NAME(type,0),"FALSE"))))
  2029.      TYPE_CODE(type) = TYPE_CODE_BOOL;
  2030. #endif
  2031.   return type;
  2032. }
  2033.  
  2034. struct section_offsets *
  2035. coff_symfile_offsets (objfile, addr)
  2036.      struct objfile *objfile;
  2037.      CORE_ADDR addr;
  2038. {
  2039.   struct section_offsets *section_offsets;
  2040.   int i;
  2041.  
  2042.   objfile->num_sections = SECT_OFF_MAX;
  2043.   section_offsets = (struct section_offsets *)
  2044.     obstack_alloc (&objfile -> psymbol_obstack,
  2045.            sizeof (struct section_offsets)
  2046.            + sizeof (section_offsets->offsets) * (SECT_OFF_MAX-1));
  2047.  
  2048.   for (i = 0; i < SECT_OFF_MAX; i++)
  2049.     ANOFFSET (section_offsets, i) = addr;
  2050.   
  2051.   return section_offsets;
  2052. }
  2053.  
  2054. /* Register our ability to parse symbols for coff BFD files */
  2055.  
  2056. static struct sym_fns coff_sym_fns =
  2057. {
  2058.   bfd_target_coff_flavour,
  2059.   coff_new_init,    /* sym_new_init: init anything gbl to entire symtab */
  2060.   coff_symfile_init,    /* sym_init: read initial info, setup for sym_read() */
  2061.   coff_symfile_read,    /* sym_read: read a symbol file into symtab */
  2062.   coff_symfile_finish,    /* sym_finish: finished with file, cleanup */
  2063.   coff_symfile_offsets, /* sym_offsets:  xlate external to internal form */
  2064.   NULL            /* next: pointer to next struct sym_fns */
  2065. };
  2066.  
  2067. void
  2068. _initialize_coffread ()
  2069. {
  2070.   add_symtab_fns(&coff_sym_fns);
  2071. }
  2072.